package com.swipemaster.activities;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.analytics.tracking.android.EasyTracker;
import com.swipemaster.R;
import com.swipemaster.common.Bitmaps;
import com.swipemaster.common.Global;
import com.swipemaster.network.task.ShowRecordsTask;
import com.swipemaster.record.RecordId;
import com.swipemaster.record.RecordsManager;

public class HighScoresActivity extends CommonActivity
{
	private final RelativeLayout[] layouts = new RelativeLayout[4];
	private ScrollView scrollView;

	@Override
	public void onBackPressed()
	{
		super.onBackPressed();
		overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		setContentView(R.layout.high_scores);
		EasyTracker.getInstance().activityStart(this);

		findViewById(R.id.high_scores_root).setBackgroundDrawable(Bitmaps.highScoresBg);

		RecordsManager.updateTextViews(this);

		final Button challengeButton = (Button) findViewById(R.id.challenge_button);
		final Button customGameButton = (Button) findViewById(R.id.custom_game_button);
		final Button backButton = (Button) findViewById(R.id.back_high_scores);
		backButton.setTypeface(Global.app.consts.font);

		scrollView = (ScrollView) findViewById(R.id.high_scores_scroll);
		layouts[0] = (RelativeLayout) findViewById(R.id.scores_relative0);
		layouts[1] = (RelativeLayout) findViewById(R.id.scores_relative1);
		layouts[2] = (RelativeLayout) findViewById(R.id.scores_relative2);
		layouts[3] = (RelativeLayout) findViewById(R.id.scores_relative3);

		for (final RelativeLayout layout : layouts)
		{
			for (int i = 0; i < layout.getChildCount(); i++)
			{
				final View view = layout.getChildAt(i);
				if ((view instanceof TextView) && !(view instanceof Button))
				{
					final TextView text = (TextView) view;
					text.setTypeface(Global.app.consts.font);
				}
			}
		}

		challengeButton.setOnClickListener(getListener(challengeButton, new RecordId(RecordId.CHALLENGE)));
		customGameButton.setOnClickListener(getListener(customGameButton, new RecordId(RecordId.CUSTOM_GAME)));

		backButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				onBackPressed();
			}
		});

		setAnimations(true);

		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onStop()
	{
		super.onStop();
		EasyTracker.getInstance().activityStop(this);
	}

	private OnClickListener getListener(final Button button, final RecordId recordId)
	{
		return new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				new ShowRecordsTask(HighScoresActivity.this, recordId).execute();
			}
		};
	}

	private void setAnimations(boolean in)
	{
		scrollView.invalidate();
		for (final RelativeLayout layout : layouts)
		{
			if (in)
			{
				layout.setAnimation(AnimationUtils.loadAnimation(getBaseContext(), R.anim.fade_in));
			}
			else
			{
				layout.setAnimation(AnimationUtils.loadAnimation(getBaseContext(), R.anim.fade_out));
			}
		}
		if (!in)
		{
			setVisible(View.INVISIBLE);
		}
	}

	private void setVisible(int visible)
	{
		for (final RelativeLayout layout : layouts)
		{
			layout.setVisibility(visible);
		}
	}
}
